unit UntFormFiltro;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UntBaseCadastro, cxGraphics, cxLookAndFeels, cxLookAndFeelPainters,
  Menus, StdCtrls, cxButtons, ExtCtrls, cxControls, cxContainer, cxEdit,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinFoggy, dxSkinGlassOceans, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSharp, dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinXmas2008Blue, cxLabel,
  cxTextEdit, cxMaskEdit, cxButtonEdit;

type
  TFrmFiltro = class(TFrmBaseCadastro)
    lblNumeroAcerto: TcxLabel;
    edtNumeroAcerto: TcxButtonEdit;
    procedure edtNumeroAcertoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtNumeroAcertoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure btnSalvarClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    function  getCheckSelecao(pTabela, pCampoId, pCampoList, pFiltro, pSelecionado: string): string;
    { Public declarations }
  end;

var
  FrmFiltro: TFrmFiltro;

implementation
uses UntFormCheckSelecao, untDataModule;
{$R *.dfm}

procedure TFrmFiltro.btnSalvarClick(Sender: TObject);
begin
  inherited;
  try
    Screen.Cursor := crSQLWait;
    DM.qAcerto_Mostruario.SQL.Clear;
    DM.qAcerto_Mostruario.SQL.Add('SELECT * FROM ACERTO_MOSTRUARIO WHERE ID_VENDA_PSJ = ' + edtNumeroAcerto.Text);
    DM.cdsAcerto_Mostruario.Close;
    DM.cdsAcerto_Mostruario.Open;

    DM.cdsItem_Acerto_Mostruario.Close;
    DM.cdsItem_Acerto_Mostruario.Open;

    DM.cdsEmpresa.Close;
    DM.cdsEmpresa.Open;

    DM.qVenda_Semi_Joia.SQL.Clear;
    DM.qVenda_Semi_Joia.SQL.Add('SELECT * FROM VENDA_PSJ WHERE ID_VENDA_PSJ = ' + edtNumeroAcerto.Text);
    DM.cdsVenda_Semi_Joia.Close;
    DM.cdsVenda_Semi_Joia.Open;

    DM.cdsItem_Venda_Semi_Joia.Close;
    DM.cdsItem_Venda_Semi_Joia.Open;

    //Preenchendo Dataset do fechamento
    DM.cdsItem_Venda_Semi_Joia.First;
    DM.cdsItem_Fechamento.Close;
    DM.cdsItem_Fechamento.Open;
    DM.cdsItem_Fechamento.EmptyDataSet;
    while not DM.cdsItem_Venda_Semi_Joia.eof do
    begin
      if not DM.cdsItem_Acerto_Mostruario.Locate('REFERENCIA', DM.cdsItem_Venda_Semi_JoiaREFERENCIA.AsString,[]) then
      begin
        DM.cdsItem_Fechamento.Insert;
        DM.cdsItem_FechamentoPRODUTO.AsString       := DM.cdsItem_Venda_Semi_JoiaDESCRICAO.AsString;
        DM.cdsItem_FechamentoREFERENCIA.AsString    := DM.cdsItem_Venda_Semi_JoiaREFERENCIA.AsString;
        DM.cdsItem_FechamentoQUANTIDADE.AsInteger   := DM.cdsItem_Venda_Semi_JoiaQUANTIDADE.AsInteger;
        DM.cdsItem_FechamentoFATOR.AsCurrency       := DM.cdsItem_Venda_Semi_JoiaFATOR.AsCurrency;
        DM.cdsItem_FechamentoVALOR.AsCurrency       := DM.cdsItem_Venda_Semi_JoiaVALOR_TOTAL.AsCurrency;
        DM.cdsItem_FechamentoVALOR_TOTAL.AsCurrency := DM.cdsItem_Venda_Semi_JoiaVALOR_TOTAL.AsCurrency * DM.cdsItem_Venda_Semi_JoiaQUANTIDADE.AsInteger;

        DM.cdsItem_Fechamento.Post;
      end;
      DM.cdsItem_Venda_Semi_Joia.Next;
    end;
  finally
    Screen.Cursor := crDefault;
  end;

  try
    if (DM.cdsAcerto_Mostruario.Active)      and (DM.cdsAcerto_Mostruario.RecordCount > 0)      and
//         (DM.cdsItem_Fechamento.Active)        and (DM.cdsItem_Fechamento.RecordCount > 0)        and
       (DM.cdsEmpresa.Active)                and (DM.cdsEmpresa.RecordCount > 0)                and
       (DM.cdsVenda_Semi_Joia.Active)        and (DM.cdsVenda_Semi_Joia.RecordCount > 0)        and
       (DM.cdsItem_Venda_Semi_Joia.Active)   and (DM.cdsItem_Venda_Semi_Joia.RecordCount > 0)   then
//         (DM.cdsItem_Acerto_Mostruario.Active) and (DM.cdsItem_Acerto_Mostruario.RecordCount > 0) then
      DM.frxAcerto_Mostruario.ShowReport;
  finally

  end;
end;

procedure TFrmFiltro.edtNumeroAcertoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
  if Key = 116 then //F5
    edtNumeroAcertoPropertiesButtonClick(edtNumeroAcerto, 0);
end;

procedure TFrmFiltro.edtNumeroAcertoPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  inherited;
  edtNumeroAcerto.Text := getCheckSelecao('VENDA_PSJ','ID_VENDA_PSJ','NUMERO_MOSTRUARIO||'+QuotedStr('-')+'||COALESCE(NOME_VENDEDOR_PROMOTOR,'+QuotedStr(' ')+')||'+QuotedStr('-')+'||DATA_EMISSAO AS CAMPO',' AND SITUACAO = '+QuotedStr('F'),edtNumeroAcerto.Text);
end;

procedure TFrmFiltro.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    Perform(Wm_NextDlgCtl,0,0);
  end;
end;

function TFrmFiltro.getCheckSelecao(pTabela, pCampoId, pCampoList, pFiltro,
  pSelecionado: string): string;
var
  vFrmFormCheckSelecao : TFrmFormCheckSelecao;
begin
  Result := '';
  vFrmFormCheckSelecao := TFrmFormCheckSelecao.Create(nil);
  try
    vFrmFormCheckSelecao.vTabela        := pTabela;
    vFrmFormCheckSelecao.vCampoId       := pCampoId;
    vFrmFormCheckSelecao.vCampoListagem := pCampoList;
    vFrmFormCheckSelecao.vFiltro        := pFiltro;
    vFrmFormCheckSelecao.vSelecionado   := pSelecionado;

    if vFrmFormCheckSelecao.ShowModal = mrOK then
      Result := vFrmFormCheckSelecao.vListSelecao
    else
      Result := pSelecionado;
  finally
    if Pos(',',Result) > 0 then
      Result := Copy(Result,1,Pos(',',Result)-1);
    vFrmFormCheckSelecao.Free;
  end;
end;

end.
